package com.windoent.yugioh.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.windoent.yugioh.R;
import com.windoent.yugioh.bean.GeneralBean;
import com.windoent.yugioh.bean.OnGoingDuiZhenBean;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.ui.activity.saishi.PanFaDetailsActivity;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.SharedPreUtil;
import com.windoent.yugioh.utils.SimpleUtils;
import com.windoent.yugioh.utils.TDevice;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.RoundImageView;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import okhttp3.Call;

/**
 * 作者：Android
 * 时间：2022/1/16 11:08
 * 功能：繼承Dialog，自定义更新Dialog
 */
public class AuthSubmitGradeDialog extends Dialog {
    private Context mContext;
    private RoundImageView mIvAvatarLeft;
    private TextView mTvNameLeft;
    private TextView mTvIdLeft;
    private RoundImageView mIvAvatarRight;
    private TextView mTvNameRight;
    private TextView mTvIdRight;

    private TextView mTvUnselectVictory;
    private TextView mTvSelectedVictory;
    private ImageView mIvVictory;
    private TextView mTvUnselectPing;
    private TextView mTvSelectedPing;
    private TextView mTvUnselectFail;
    private TextView mTvSelectedFail;
    private ImageView mIvFail;
    private TextView mTvNo;
    private TextView mTvYes;
    private RelativeLayout mLlVictorySelected;
    private RelativeLayout mLlPingSelected;
    private RelativeLayout mLlFailSelected;
    private RelativeLayout mRlAvatarLeft;
    private ImageView mIvSucessBgLeft;
    private ImageView mIvSucessBgLeftHuangguan;
    private RelativeLayout mRlAvatarRight;
    private ImageView mIvSucessBgRightHuangguan;
    private ImageView mIvSucessBgRight;
    private RelativeLayout mLlDoubleDefeatSelected;
    private TextView mTvUnselectDoubleDefeat;
    private TextView mTvSelectedDoubleDefeat;
    private ImageView mIvDoubleDefeat;
    private LinearLayout mLlLeft;
    private LinearLayout mLlRight;


    private int selectPosition = 0;//1左胜利，2右胜，3平,4双败

    private onClickListener listener;
    private OnGoingDuiZhenBean.DataBean.ResBean info;
    private String gameId;
    private boolean taoTaiSaiGame = false;
    private String statusid="";

    public void setOnclickListener(onClickListener clickListener) {
        this.listener = clickListener;
    }

    public AuthSubmitGradeDialog(Context context) {
        super(context, R.style.MoHUDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.free_sumit_grade_auth_dialog_layout);
        //按空白处能取消动画
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.BOTTOM);
        window.setAttributes(attributes);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        mLlVictorySelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSelectText(true, false, false, false);
                selectPosition = 1;
                showSucessHuangGuan(true, false);
            }
        });
        mLlPingSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSelectText(false, true, false, false);
                selectPosition = 3;
                showSucessHuangGuan(true, true);
            }
        });
        mLlFailSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSelectText(false, false, true, false);
                selectPosition = 2;
                showSucessHuangGuan(false, true);
            }
        });
        mLlDoubleDefeatSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showSelectText(false, false, false, true);
                selectPosition = 4;
                showSucessHuangGuan(false, false);
            }
        });
        mTvYes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (selectPosition == 0) {
                    ToastUtils.showText(getContext().getString(R.string.string_select_one_to_submit));
                    return;
                }
                if (listener != null) {
                    listener.onClick(selectPosition);
                }
                dismiss();
            }
        });
        mTvNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        mLlLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (info != null) {
                    getPanFaStyleList(info.getA_nickname(), gameId, info.getA_id() + "", info.getId() + "");
                }
            }
        });
        mLlRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (info != null) {
                    getPanFaStyleList(info.getB_nickname(), gameId, info.getB_id() + "", info.getId() + "");
                }
            }
        });
    }

    //获取usertoken
    private String getUserToken() {
        return SharedPreUtil.getString(mContext, Constants.APPTOKEN);
    }

    //判罚类型列表
    private void getPanFaStyleList(String name, String gameId, String userId, String vid) {
        PhoneLive.getPanFaStyleList(getUserToken(), gameId, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            showUserPanFaDialog(name, gameId, userId, response, vid);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtils.showText(getContext().getString(R.string.string_response_error));
                }
            }
        });
    }

    //用户对阵判罚记录
    private void showUserPanFaDialog(String name, String gameId, String userId, String response, String vId) {
        PanFaUserDetailsDialog dialog = new PanFaUserDetailsDialog(mContext);
        dialog.setUserStr(name, gameId, userId, response);
        dialog.setOnclickListener(new PanFaUserDetailsDialog.onClickListener() {
            @Override
            public void onClickSure(String type_id, String remark) {
                panfaPlayers(vId, userId, type_id, remark);
            }

            @Override
            public void clickMore() {
                Intent intent = new Intent(mContext, PanFaDetailsActivity.class);
                intent.putExtra("gameId", gameId);
                intent.putExtra("userId", userId);
                mContext.startActivity(intent);
            }
        });
        dialog.show();
    }

    //判罚操作
    private void panfaPlayers(String vid, String userId, String type_id, String remark) {
        PhoneLive.panfaPlayers(getUserToken(), vid, userId, type_id, remark,statusid, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                if (BaseUtils.isJSONValid(response)) {
                    Gson gson = new Gson();
                    GeneralBean fromJson = gson.fromJson(response, GeneralBean.class);
                    if (fromJson != null) {
                        int code = fromJson.getCode();
                        String msg = fromJson.getMsg();
                        if (code == 200) {
                            BaseMethod.showSucessDialog(mContext, BaseMethod.notNullDefaultValue(msg, mContext.getString(R.string.string_panfa_player_sucess)), new SucessFailAutoDismissDialog.OnInterface() {
                                @Override
                                public void onDissmiss() {

                                }
                            });
                        } else {
                            BaseMethod.showFailDialog(mContext, mContext.getString(R.string.string_panfa_player_fail), BaseMethod.notNullDefaultValueNull(msg));
                        }
                    }
                } else {
                    ToastUtils.showText(mContext.getString(R.string.string_response_error));
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        showSelectText(false, false, false, false);
        setUi(info);
        showOutGameUi(taoTaiSaiGame);
    }

    private void setUi(OnGoingDuiZhenBean.DataBean.ResBean info) {
        if (info == null) {
            showSucessHuangGuan(false, false);
            return;
        }
        SimpleUtils.loadImageForView(getContext(), mIvAvatarLeft, info.getA_avatar(), R.drawable.img_official_avatar_android);
        SimpleUtils.loadImageForView(getContext(), mIvAvatarRight, info.getB_avatar(), R.drawable.img_official_avatar_android);
        mTvNameLeft.setText(BaseMethod.notNullDefaultValueNull(info.getA_nickname()));
        mTvNameRight.setText(BaseMethod.notNullDefaultValueNull(info.getB_nickname()));
        mTvIdLeft.setText(getContext().getString(R.string.string_user_info_id) + BaseMethod.notNullDefaultValueNull(info.getA_id() + ""));
        mTvIdRight.setText(getContext().getString(R.string.string_user_info_id) + BaseMethod.notNullDefaultValueNull(info.getB_id() + ""));
        int a_result = info.getA_result();
        int b_result = info.getB_result();
        showSucessHuangGuan(a_result == 1 || a_result == 2, b_result == 1 || b_result == 2);
        // "0": "还未提交成绩","1": "胜", "2": "平","3": "负","4": "双负"
        showSelectText(a_result == 1 && b_result == 3, a_result == 2 && b_result == 2, a_result == 3 && b_result == 1, a_result == 4 && b_result == 4);
        if (a_result == 1 && b_result == 3) {
            selectPosition = 1;
        } else if (a_result == 2 && b_result == 2) {
            selectPosition = 3;
        } else if (a_result == 3 && b_result == 1) {
            selectPosition = 2;
        } else if (a_result == 4 && b_result == 4) {
            selectPosition = 4;
        }
    }

    //胜利者头像
    private void showSucessHuangGuan(boolean a, boolean b) {
        if (a && b) {
            mIvSucessBgLeft.setVisibility(View.VISIBLE);
            mIvSucessBgLeftHuangguan.setVisibility(View.VISIBLE);
            mIvSucessBgRightHuangguan.setVisibility(View.VISIBLE);
            mIvSucessBgRight.setVisibility(View.VISIBLE);
        } else if (a) {
            mIvSucessBgLeft.setVisibility(View.VISIBLE);
            mIvSucessBgLeftHuangguan.setVisibility(View.VISIBLE);
            mIvSucessBgRightHuangguan.setVisibility(View.INVISIBLE);
            mIvSucessBgRight.setVisibility(View.INVISIBLE);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mRlAvatarRight.getLayoutParams();
            layoutParams.rightMargin = TDevice.dp2px(getContext(), 10);
            mRlAvatarRight.setLayoutParams(layoutParams);
        } else if (b) {
            mIvSucessBgLeft.setVisibility(View.INVISIBLE);
            mIvSucessBgLeftHuangguan.setVisibility(View.INVISIBLE);
            mIvSucessBgRightHuangguan.setVisibility(View.VISIBLE);
            mIvSucessBgRight.setVisibility(View.VISIBLE);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mRlAvatarLeft.getLayoutParams();
            layoutParams.rightMargin = TDevice.dp2px(getContext(), 10);
            mRlAvatarLeft.setLayoutParams(layoutParams);
        } else {
            mIvSucessBgLeft.setVisibility(View.INVISIBLE);
            mIvSucessBgLeftHuangguan.setVisibility(View.INVISIBLE);
            mIvSucessBgRightHuangguan.setVisibility(View.INVISIBLE);
            mIvSucessBgRight.setVisibility(View.INVISIBLE);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mRlAvatarLeft.getLayoutParams();
            layoutParams.rightMargin = TDevice.dp2px(getContext(), 10);
            mRlAvatarLeft.setLayoutParams(layoutParams);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mRlAvatarRight.getLayoutParams();
            lp.rightMargin = TDevice.dp2px(getContext(), 10);
            mRlAvatarRight.setLayoutParams(lp);
        }
    }

    /**
     * 初始化界面控件
     */
    private void initView() {

        mIvAvatarLeft = (RoundImageView) findViewById(R.id.iv_avatar_left);
        mTvNameLeft = (TextView) findViewById(R.id.tv_name_left);
        mTvIdLeft = (TextView) findViewById(R.id.tv_id_left);
        mIvAvatarRight = (RoundImageView) findViewById(R.id.iv_avatar_right);
        mTvNameRight = (TextView) findViewById(R.id.tv_name_right);
        mTvIdRight = (TextView) findViewById(R.id.tv_id_right);
        mTvUnselectVictory = (TextView) findViewById(R.id.tv_unselect_victory);
        mTvSelectedVictory = (TextView) findViewById(R.id.tv_selected_victory);
        mIvVictory = (ImageView) findViewById(R.id.iv_victory);
        mTvUnselectPing = (TextView) findViewById(R.id.tv_unselect_ping);
        mTvSelectedPing = (TextView) findViewById(R.id.tv_selected_ping);
        mTvUnselectFail = (TextView) findViewById(R.id.tv_unselect_fail);
        mTvSelectedFail = (TextView) findViewById(R.id.tv_selected_fail);
        mIvFail = (ImageView) findViewById(R.id.iv_fail);
        mTvNo = (TextView) findViewById(R.id.tv_no);
        mTvYes = (TextView) findViewById(R.id.tv_yes);
        mLlVictorySelected = (RelativeLayout) findViewById(R.id.ll_victory_selected);
        mLlPingSelected = (RelativeLayout) findViewById(R.id.ll_ping_selected);
        mLlFailSelected = (RelativeLayout) findViewById(R.id.ll_fail_selected);

        mRlAvatarLeft = (RelativeLayout) findViewById(R.id.rl_avatar_left);
        mIvSucessBgLeft = (ImageView) findViewById(R.id.iv_sucess_bg_left);
        mIvSucessBgLeftHuangguan = (ImageView) findViewById(R.id.iv_sucess_bg_left_huangguan);
        mRlAvatarRight = (RelativeLayout) findViewById(R.id.rl_avatar_right);
        mIvSucessBgRightHuangguan = (ImageView) findViewById(R.id.iv_sucess_bg_right_huangguan);
        mIvSucessBgRight = (ImageView) findViewById(R.id.iv_sucess_bg_right);


        mLlDoubleDefeatSelected = (RelativeLayout) findViewById(R.id.ll_double_defeat_selected);
        mTvUnselectDoubleDefeat = (TextView) findViewById(R.id.tv_unselect_double_defeat);
        mTvSelectedDoubleDefeat = (TextView) findViewById(R.id.tv_selected_double_defeat);
        mIvDoubleDefeat = (ImageView) findViewById(R.id.iv_double_defeat);

        mLlLeft = (LinearLayout) findViewById(R.id.ll_left);
        mLlRight = (LinearLayout) findViewById(R.id.ll_right);
    }


    public interface onClickListener {
        public void onClick(int selectPosition);
    }

    //选中显示
    private void showSelectText(boolean victory, boolean ping, boolean fail, boolean doubleDefeat) {
        mTvUnselectVictory.setVisibility(victory ? View.INVISIBLE : View.VISIBLE);
        mTvSelectedVictory.setVisibility(!victory ? View.INVISIBLE : View.VISIBLE);
        mIvVictory.setVisibility(!victory ? View.INVISIBLE : View.VISIBLE);
        mTvUnselectPing.setVisibility(ping ? View.INVISIBLE : View.VISIBLE);
        mTvSelectedPing.setVisibility(!ping ? View.INVISIBLE : View.VISIBLE);
        mTvUnselectFail.setVisibility(fail ? View.INVISIBLE : View.VISIBLE);
        mTvSelectedFail.setVisibility(!fail ? View.INVISIBLE : View.VISIBLE);
        mIvFail.setVisibility(!fail ? View.INVISIBLE : View.VISIBLE);
        mTvUnselectDoubleDefeat.setVisibility(doubleDefeat ? View.GONE : View.VISIBLE);
        mTvSelectedDoubleDefeat.setVisibility(!doubleDefeat ? View.GONE : View.VISIBLE);
        mIvDoubleDefeat.setVisibility(!doubleDefeat ? View.GONE : View.VISIBLE);
    }

    private void showOutGameUi(boolean taoTaiSaiGame) {
        mLlPingSelected.setVisibility(taoTaiSaiGame?View.GONE:View.VISIBLE);
        mLlDoubleDefeatSelected.setVisibility(taoTaiSaiGame?View.GONE:View.VISIBLE);
    }

    public void setInfo(OnGoingDuiZhenBean.DataBean.ResBean bean, String gameId, boolean outGame,String statusid) {
        this.info = bean;
        this.gameId = gameId;
        this.taoTaiSaiGame = outGame;
        this.statusid = statusid;
    }
}
